package labmanager.utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import labmanager.business.LabManagerTypeReservation;
import labmanager.business.LabManagerUser;
import org.apache.log4j.Logger;

import dto.ResponseDTO;
import dto.UserDTO;

public class AccessDB {
	
	private static Logger logger = Logger.getLogger(AccessDB.class);
	
	private String db = "labmanager";
	private String url = "jdbc:mysql://localhost/"+db;
	private String user = "root";
	private String pass = "root";

	public Connection connect() {
		Connection link = null; 
		try{   
			Class.forName("org.gjt.mm.mysql.Driver");
			link = DriverManager.getConnection(this.url, this.user, this.pass);
			return link;
			 
	    }catch(Exception ex){
	    	logger.debug("No es posible realiazar la conexion a la base de datos.");
	    }
		return link; 	
	}
	
	public ResultSet executeQuery(String query) {                                                  
		AccessDB mysql = new AccessDB();
        java.sql.Connection cn= mysql.connect();
        if(cn!=null){
            try{
            	Statement instrution = cn.createStatement();
            	ResultSet result = instrution.executeQuery(query);
            	return result;
            }catch(SQLException ex){
                System.out.println("Error de acceso a la base de datos " + ex);
            }
        }
		return null;
    }
	
	public int executeQueryDML(String query) {                                                  
		AccessDB mysql = new AccessDB();
        java.sql.Connection cn= mysql.connect();
        if(cn!=null){
            try{
            	Statement instrution = cn.createStatement();
            	 int result = instrution.executeUpdate(query);
            	return result;
            }catch(SQLException ex){
                System.out.println("Error de acceso a la base de datos " + ex);
            }
        }
		return 0;
    }
	
	public static void main(String args[]) throws SQLException{
		
		// Test crear o actulizar usuario
		/* 
		UserDTO user = new UserDTO();
		user.setCode(1012349084);
		user.setName("Juliana");
		user.setLastName("Rodriguez");
		user.setUserName("jrodriguez1");
		user.setPassword("");
		user.setState(1);
		user.setProfileId(3);
		LabManagerUser labmanager = new LabManagerUser();
		ResponseDTO x = labmanager.updateUser(user);*/ 
		// Test login
		/*
		LabManagerUser labmanager = new LabManagerUser();
		ResponseDTO x = labmanager.loginUser("jpiram", "81dc9bdb52d04dc20036dbd8313ed055"); */
//		System.out.println(x.getMessage() + x.getCode()); 
		// Test listar usuarios		
		/*LabManagerTypeReservation labmanager = new LabManagerTypeReservation();
		ArrayList<String> x = labmanager.listTypeReservation("1 = 1");
		for(int i=0; i<x.size(); i++){
			System.out.println(x.get(i));
		}*/
	}

}
